#include	"unp.h"

#define MAX_SIZE 4096
int
main(int argc, char **argv)
{
	int					sockfd, connfd,filefd, readsize;
	char				buff[MAX_SIZE], filename[MAX_SIZE];
	struct sockaddr_in	servaddr;

	if (argc != 4)
		err_quit("usage: tcpcli <IPaddress> <Port> <Filename>");

	if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
		err_sys("socket error");

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port   = htons(atol(argv[2]));	/* daytime server */
	if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
		err_quit("inet_pton error for %s", argv[1]);

	if ((connfd = connect(sockfd, (SA *) &servaddr, sizeof(servaddr))) < 0)
		err_sys("connect error");

	sprintf(filename,"%s",argv[3]);
	writen(sockfd,filename ,MAX_SIZE);

	if((filefd = open(filename,O_RDONLY)) < 0)
		err_sys("file open error");

	while((readsize=readline(filefd,buff,sizeof(buff))) > 0)
		writen(sockfd,buff,sizeof(buff));
	close(filefd);
	exit(0);
}
